package com.wzgl.core.utils;

import com.jfinal.plugin.redis.Redis;

public class WZGLUtils {
	
	public static String getCacheName(String cacheName,String key){
		StringBuilder name = new StringBuilder();
		if(StringUtils.isNotEmpty(cacheName)){
			name.append(cacheName).append(":");
		}
		if(StringUtils.isNotEmpty(key)){
			name.append(key);
		}else{
			name.append("wzgl").append(":").append(UuidUtil.get32UUID());
		}
		return name.toString();
	}
	
	/**
	 * 自定义 -获取缓存
	 * @param cacheName 名称前缀
	 * @param key 名称后缀
	 * @return 返回object缓存对象
	 */
	public static Object getCache(String cacheName,Object key){
		StringBuilder name = new StringBuilder();
		if(StringUtils.isNotEmpty(cacheName)){
			name.append(cacheName).append(":");
		}
		if(key != null){
			name.append(key);
		}else{
			name.append("wzgl").append(":").append(UuidUtil.get32UUID());
		}
		return Redis.use(cacheName).get(name.toString());
	}
	
	/**
	 * 存入redis
	 * @param cacheName 缓存名称
	 * @param name 键
	 * @param value 值
	 */
	public static void setCache(String cacheName,String name,Object value){
		if(StringUtils.isNotEmpty(cacheName)){
			Redis.use(cacheName).set(name, value);
		} 
	}
	
	/**
	 * 判断是否存在此键
	 * @param cachename 缓存名称
	 * @param key 键
	 * @return 
	 */
	public static boolean exists(String cachename,String key){
		if("".equals(key) || key == null || "".equals(cachename) || cachename == null){
			return false;
		}
		return Redis.use(cachename).exists(key);
	}
	
	public static void del(String cachename ,String key){
		Redis.use(cachename).del(key);
	}
}
